<?php

namespace app\admin\controller;

use support\Request;
use support\Response;
use Exception;
use app\model\BizPayRecordModel;
use app\model\BizGoodsOrderModel;
use app\model\OrderModel;
use app\model\UserModel;
use app\model\CardUserModel;

class DataController
{

    public function today(Request $request): Response
    {
        try {
            $today = strtotime(date('Y-m-d'));

            // 今日随心付
            $bizPayRecordTodayTotalPayMoney = BizPayRecordModel::where('paid_at', '>=', $today)
                ->where('paid_at', '<', $today + 86400)
                ->where('pay_status', '=', 1)
                ->sum('pay_money');

            // 随心付总金额
            $bizPayRecordTotal = BizPayRecordModel::where('pay_status', '=', 1)
                ->sum('pay_money');

            // 今日商家本地生活实付金额
            $bizOrderTodayTotalPayMoney = BizGoodsOrderModel::where('paid_at', '>=', $today)
                ->where('paid_at', '<', $today + 86400)
                ->where('status', '=', 'pay')
                ->sum('pay_money');
            // 商家本地生活实付金额
            $bizOrderTotalPayMoney = BizGoodsOrderModel::where('status', '=', 'pay')
                ->sum('pay_money');

            // 今日严选实付金额
            $orderTodayTotalPayMoney = OrderModel::where('pay_time', '>=', $today)
                ->where('pay_time', '<', $today + 86400)
                ->where('pay_status', '=', 1)
                ->sum('pay_amount');
            // 严选实付金额
            $orderTotalPayMoney = OrderModel::where('pay_status', '=', 1)
                ->sum('pay_amount');

            // 今日会员新增
            $userTodayTotal = UserModel::where('created_at', '>=', $today)
                ->where('created_at', '<', $today + 86400)
                ->count();
            // 会员新增
            $userTotal = UserModel::count();

            $data = [
                'bizPayRecordTodayTotalPayMoney' => round($bizPayRecordTodayTotalPayMoney, 2),
                'bizPayRecordTotal' => round($bizPayRecordTotal, 2),

                'bizOrderTodayTotalPayMoney' => round($bizOrderTodayTotalPayMoney, 2),
                'bizOrderTotalPayMoney' => round($bizOrderTotalPayMoney, 2),

                'orderTotalPayMoney' => round($orderTotalPayMoney, 2),
                'orderTodayTotalPayMoney' => round($orderTodayTotalPayMoney, 2),

                'userTodayTotal' => $userTodayTotal,
                'userTotal' => $userTotal,
            ];

            return json([
                'data' => $data,
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => '获取失败',
                'code' => 0
            ]);
        }
    }

    /**
     * 云豆使用情况
     * @param \support\Request $request
     * @return Response
     */
    public function scoreReport(Request $request): Response
    {
        try {
            $unUsedUserCreditScore = UserModel::sum('score');
            $unUsedUserCardScore = CardUserModel::sum('score');

            $usedUserCardScore = CardUserModel::sum('use_score');
            

            return json([
                'data' => [
                    [
                        'name' => '未使用',
                        'value' => round(($unUsedUserCreditScore + $unUsedUserCardScore), 2),
                    ],
                    [
                        'name' => '已使用',
                        'value' => round(($usedUserCardScore), 2),
                    ]
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => '获取失败',
                'code' => 0
            ]);
        }
    }

    public function userReport(Request $request): Response
    {
        try {
            $userTotal = UserModel::count();
            $userVipTotal = UserModel::where('vip_at', '>', time())->count();
            $userCompanyTotal = UserModel::where('company_id', '>', 0)->count();

            $bindPhoneUserTotal = UserModel::whereNotNull('phone')->count();

            return json([
                'msg' => '获取成功',
                'code' => 20000,
                'data' => [
                    [
                        'name' => '总数',
                        'value' => $userTotal,
                    ],
                    [
                        'name' => 'VIP',
                        'value' => $userVipTotal,
                        
                    ],
                    [
                        'name' => '单位员工',
                        'value' => $userCompanyTotal,
                        
                    ],
                    [
                        'name' => '有效用户',
                        'value' => $bindPhoneUserTotal,
                        
                    ]
                ],
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => '获取失败',
                'code' => 0
            ]);
        }
    }
}
